package com.adwin.addcoin.ui

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.adwin.addcoin.AddCoinApplication
import com.adwin.addcoin.BuildConfig
import com.adwin.addcoin.R
import com.adwin.addcoin.repository.SchedulerTransformer
import com.adwin.addcoin.repository.api.TestApi
import com.adwin.addcoin.repository.net.RetrofitClient
import com.adwin.addcoin.ui.dialog.GaidDialog
import com.adwin.addcoin.ui.dialog.UserAgreementDialog
import com.adwin.addcoin.ui.login.LoginActivity
import com.adwin.addcoin.ui.main.MainActivity
import com.adwin.addcoin.utils.DevicesUtils
import com.adwin.addcoin.utils.PersonalMessageUtils
import com.adwin.addcoin.utils.dataStore
import com.adwin.addcoin.utils.getAgreement
import com.adwin.addcoin.utils.getToken
import com.adwin.addcoin.utils.getUserGaid
import com.adwin.addcoin.utils.installRepost
import com.adwin.addcoin.utils.log
import com.adwin.addcoin.utils.saveToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.Date
import java.util.concurrent.atomic.AtomicBoolean

class SplashActivity : AppCompatActivity() {

    private var isAgreement = AtomicBoolean(false)
    private var isCutler = AtomicBoolean(false)
    private var clokString = "checksum"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        lifecycleScope.launchWhenResumed{
            delay(2000)
            isAgreement()
        }

    }

    private fun isAgreement(){
        lifecycleScope.launch{
            val agreement = getAgreement()
            if (!agreement){
                if (isAgreement.get()) finish()
                val agreementDialog =  UserAgreementDialog(this@SplashActivity)
                agreementDialog.setOnDismissListener {
                    isAgreement.set(true)
                    isAgreement()
                }
                agreementDialog.show()
            }else{
                checkAppCloak()
            }
        }
    }

    private fun checkAppCloak(){
        val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Date())
        val androidId = getAndroidID()
        val version = Build.VERSION.RELEASE
        val model = android.os.Build.MODEL
        MainScope().launch (Dispatchers.IO){
            installRepost("checkAppCloak")
        }
        RetrofitClient.getInstance().getService(TestApi::class.java)
            .checkAppCloak(androidId,time,model,BuildConfig.APPLICATION_ID,
                version,AddCoinApplication.gaid,androidId,"android", DevicesUtils.map[DevicesUtils.versionName] as String,"4252cb815ff2727495585b3370b85ef0").compose(
                SchedulerTransformer()
            ).subscribe({
                clokString = it.string()
                clokString.log()
                MainScope().launch (Dispatchers.IO){
                    val jsonobt = JSONObject()
                    jsonobt.put("cloak",clokString)
                    installRepost("getCloak",jsonobt)
                }
                checkGaid()
            },{
                "${it.message}".log()
            })
    }

    private fun checkGaid(){
        AddCoinApplication.isCloak = clokString != "cutler"
        if (TextUtils.isEmpty(AddCoinApplication.gaid)){
            MainScope().launch (Dispatchers.IO){
                installRepost("showGaidDialog")
            }
            var dialog = GaidDialog(this@SplashActivity)
            dialog.setOnDismissListener {
                AddCoinApplication.instance.gaid()
                if (TextUtils.isEmpty(AddCoinApplication.gaid)&&clokString == "cutler"){
                    finish()
                }else{
                    startActivity()
                }
            }
            dialog.show()
        }else{
            startActivity()
        }
    }

    private fun startActivity(){
        MainScope().launch (Dispatchers.IO){
            val json = JSONObject()
            json.put("isCloak",AddCoinApplication.isCloak)
            installRepost("startApp",json)
        }
        lifecycleScope.launch {
            MainScope().launch (Dispatchers.IO){
                installRepost("startLoginActivity")
            }
            startActivity(Intent(this@SplashActivity, LoginActivity::class.java))
            finish()
        }
    }

    private fun getAndroidID():String {
        val androidID = Settings.System.getString(contentResolver, Settings.Secure.ANDROID_ID)
        "AndroidID为:$androidID".log()
        return androidID
    }

}